import Options

def options(opt):
  opt.add_option('--disable-re2',
                 action='store_true',
                 default=False,
                 help='do not use re2')

def configure(conf):
  if not Options.options.disable_re2:
    conf.check_cxx(lib = 're2', define_name = 'HAVE_RE2',
                   errmsg = 'not found (add "--disable-re2" option if not necessary)')

  libpat = conf.env.cxxshlib_PATTERN
  conf.define('LIBSPLITTER_SAMPLE', libpat % 'splitter_sample')
  conf.define('LIBFILTER_SAMPLE', libpat % 'filter_sample')
  conf.define('LIBNUM_FEATURE_SAMPLE', libpat % 'num_feature_sample')
  conf.define('LIBNUM_FILTER_SAMPLE', libpat % 'num_filter_sample')

def make_test(bld, use, src):
  bld.program(
    features = 'gtest',
    source = src,
    target = src[0:src.rindex('.')],
    use = use,
    )

def make_tests(bld, use, srcs):
  for src in srcs:
    make_test(bld, use, src)


def build(bld):
  source = [
    'util.cpp',
    'json_converter.cpp',
    'msgpack_converter.cpp',
    'datum_to_fv_converter.cpp',
    'space_splitter.cpp',
    'character_ngram.cpp',
    'without_split.cpp',
    'key_matcher_factory.cpp',
    'splitter_factory.cpp',
    'num_feature_factory.cpp',
    'converter_config.cpp',
    'libsvm_converter.cpp',
    'string_filter_factory.cpp',
    'num_filter_factory.cpp',
    'dynamic_loader.cpp',
    'dynamic_splitter.cpp',
    'dynamic_num_feature.cpp',
    'dynamic_string_filter.cpp',
    'dynamic_num_filter.cpp',
    'revert.cpp',
    'weight_manager.cpp',
    'keyword_weights.cpp',
    ]
  use = 'PFICOMMON MSGPACK DL'

  if bld.env.HAVE_RE2:
    source.append('re2_match.cpp')
    source.append('re2_filter.cpp')
    use += ' RE2'

  bld.shlib(
    source = source,
    target = 'jubaconverter',
    name = 'jubaconverter',
    includes = '.',
    use = use)

  n = bld.path.get_bld().make_node('test_input')
  n.mkdir()
  bld(rule = 'cp ${SRC} ${TGT}',
      source = bld.path.ant_glob('test_input/*'),
      target = n)

  bld.shlib(
    source = 'test_splitter.cpp',
    target = 'splitter_sample',
    install_path = None,
    name = 'splitter_sample',
    )

  bld.shlib(
    source = 'test_num_feature.cpp',
    target = 'num_feature_sample',
    install_path = None,
    name = 'num_feature_sample',
    )

  bld.shlib(
    source = 'test_num_filter.cpp',
    target = 'num_filter_sample',
    install_path = None,
    name = 'num_filter_sample',
    )

  bld.shlib(
    source = 'test_string_filter.cpp',
    target = 'filter_sample',
    install_path = None,
    name = 'filter_sample',
    )

  test_source = [
      'json_converter_test.cpp',
      'msgpack_converter_test.cpp',
      'datum_to_fv_converter_test.cpp',
      'space_splitter_test.cpp',
      'character_ngram_test.cpp',
      'key_matcher_test.cpp',
      'key_matcher_factory_test.cpp',
      'splitter_factory_test.cpp',
      'num_feature_factory_test.cpp',
      'converter_config_test.cpp',
      'libsvm_converter_test.cpp',
      'dynamic_splitter_test.cpp',
      'dynamic_num_feature_test.cpp',
      'dynamic_string_filter_test.cpp',
      'dynamic_num_filter_test.cpp',
      'string_filter_factory_test.cpp',
      'num_filter_impl_test.cpp',
      'num_filter_factory_test.cpp',
      'dynamic_loader_test.cpp',
      'counter_test.cpp',
      'revert_test.cpp',
      'weight_manager_test.cpp',
      'keyword_weights_test.cpp',
      ]
  test_use = 'PFICOMMON MSGPACK jubaconverter'

  if bld.env.HAVE_RE2:
    test_source.append('re2_match_test.cpp')
    test_source.append('re2_filter_test.cpp')

  make_tests(bld, test_use, test_source)

  bld.install_files('${PREFIX}/include/jubatus/fv_converter',
                    [ 'word_splitter.hpp',
                      'string_filter.hpp',
                      'num_feature.hpp',
                      'num_filter.hpp',
                      'datum_to_fv_converter.hpp',
                      'datum.hpp',
                      'converter_config.hpp',
                      'json_converter.hpp',
                      'exception.hpp'])

